package edu.put.sk.nrpp.ui;

import java.util.ArrayList;

import edu.put.sk.nrpp.Connection;
import edu.put.sk.nrpp.ConnectionList;

/**
 * LSA class representation
 * @author Tomasz Bartoszewski
 * TODO: Change static array to dynamic (eg. java.util.ArrayList, java.util.Hashtable etc. - standard java structures)	/tb
 * TODO: Write (or test only) serialization (to save status of system into file)	/mw
 * @see java.net.Inet4Address
 */

public class Router {
	public String id;			//unique router ID
	public String hostname;	//if definied?
	public Integer areaNumber;
	public Integer membership;
	public Integer numberOfNeighbors;
	public ArrayList<Connection> connections=new ArrayList<Connection>();
	public GraphicsRouter graphicsRouter;
	public ConnectionList connectionList;
	public Integer uniqeProgramNumber;
	
	/*public static final int FASTETHERNET = 0;    can we throw this away? I make new class Connection
	public static final int SERIAL = 1;
	public static final int DEFAULTROUTE = 2;*/
	

	
	
	/**
	 * 
	 * @param name	router hostname
	 * @param areaNum area ID (in OSPF integer) but in ISIS it's string (probably)
	 * @param membership1 (only one?)
	 * @param size (size of what?)
	 * TODO: Remove static arrays (implements dynamics structures)
	 */
	public Router(ConnectionList connectionList,String name,String id,Integer areaNum, Integer membership1, Integer programNumber) {
		this.connectionList=connectionList;
		numberOfNeighbors=0;
		hostname=name;
		areaNumber = areaNum;
		membership = membership1;
		this.id=id;
		this.uniqeProgramNumber = programNumber;
	}
	/**
	 * It is a fun. which make connection in(reference to object, ip of connection and type)
	 * @param router1		?
	 * @param ipA			?
	 * @param ipB			?
	 * @param mask			?
	 * @param connection	?
	 */
	/*public void symlink(Router router1,Inet4Address ipA,Inet4Address ipB,Inet4Address mask,Integer connection)
	{
		ipLinks[numberOfNeighbors]=ipA;
		router1.ipLinks[router1.numberOfNeighbors]=ipB;
		maskLinks[numberOfNeighbors]=mask;
		router1.maskLinks[router1.numberOfNeighbors]=mask;
		connectionType[numberOfNeighbors]=connection;
		router1.connectionType[router1.numberOfNeighbors]=connection;
		neighbors[numberOfNeighbors++]=router1;
		router1.neighbors[router1.numberOfNeighbors++]=this;
	}*/
	
	
	
	/**
	 * You can throw this away I us this only to verification my function
	 * ok, I change name of this method form wyswietl to show (also I remove Polish letters in text [problems with encoding])
	 * For Polish letters we must use utf-8 (set it in Eclipse: Project Properties>Resources>Text Encoding) 
	 */
	
	public void link(Router router1){
		numberOfNeighbors++;
		router1.numberOfNeighbors++;
		Connection connection = new Connection(this.connectionList, this, router1);
		this.connections.add(connection);
		router1.connections.add(connection);
	}

	public void unlink(){
		for (int i=0;i<this.connections.size();i++){
			this.connectionList.delConnection(this.connections.get(i));
		}
	}
	
	public void deleteConnection(Router router1){
		for (int i=0;i<this.connections.size();i++){
			if (this.connections.get(i).getNeighbor(this)==router1){
				this.connectionList.delConnection(this.connections.get(i));
			}
		}
	}
	
	public void linkGR(GraphicsRouter _graphicsRouter){
		this.graphicsRouter = _graphicsRouter;
	}
	
	public void show()
	{
		for (Object o: this.connections){
			Connection c=(Connection)o;
			if (!c.r1.equals(this)){
				System.out.println("Neighbors : "+c.r1.hostname);
			}
			else if (!c.r2.equals(this)){
				System.out.println("Neighbors : "+c.r2.hostname);
			}
		}
	}
	
	public ArrayList<Router> allNeighbors(){
		ArrayList<Router> returnList=new ArrayList<Router>();
		for (Object o: this.connections){
			Connection c=(Connection)o;
			if (!c.r1.equals(this)){
				returnList.add(c.r1);
			}
			else if (!c.r2.equals(this)){
				returnList.add(c.r2);
			}
		}
		return returnList;
	}
}
